/*
 * @Author: 张小平
 * @Date:   2025-05-01 
 * @Email:  qqshuqian@163.com 
 * @Copyright © 2024 aseyun. All rights reserved.
 */
import _ from 'lodash'

const branch = {
  props: {
    item: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      conditionValue: [],
      dicData: [],
      checkList: []
    }
  },
  watch: {
    conditionValue(val) {
      this.handleSave(val)
    },
    checkList(vals) {
      const had = _.filter(this.dicData, (item) => {
        const {
          value
        } = item
        return vals.includes(value)
      })
      this.handleSave(had)
    }
  },
  mounted() {
    const {
      dataType,
      propJson
    } = this.item
    if (dataType !== 'procStartUser' && propJson) {
      const obj = JSON.parse(propJson)
      if (obj?.dicData) {
        this.dicData = obj.dicData
      }
    }
  },
  methods: {
    handleDel(item) {
      this.$emit('on-del', item)
    },
    handleSave(val) {
      const {
        label,
        prop,
        dataType
      } = this.item

      const conditionValue = JSON.stringify(val)
      const outputItem = {
        conditionValue,
        fieldName: label,
        fieldProp: prop,
        type: dataType
      }

      if (this.numberCondition) {
        outputItem.numberCondition = this.numberCondition
      }

      this.$emit('on-save', outputItem)
    }
  }
}

export default branch
